/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmStokBarang.java
 *
 * Created on Jan 24, 2012, 3:36:49 PM
 */
package SIMRS.Apotek.view;

import SIMRS.Apotek.sql.KoneksiDatabase;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import usu.widget.Form;

/**
 *
 * @author yunniie
 */
public class frmStokBarang extends Form {
    Connection koneksi=KoneksiDatabase.getKoneksi();
    private DefaultTableModel model;
    /** Creates new form frmStokBarang */
    public frmStokBarang() {
        
        initComponents();
        model=new DefaultTableModel();
        tblStokBarang.setModel(model);
        model.addColumn("Nama Barang");
        model.addColumn("Nama Ruangan");
        model.addColumn("Jumlah Stok");
        loadDataStokBarang();
        
        cmbNamaBarang.setEnabled(false);
        kdBarang.setEnabled(false);
        ruangan.setEnabled(false);
        txtJumlahStok.setEnabled(false);
        cmbRuangan.setEnabled(false);
        
        cmbRuangan.addItem("");
        try {
            Statement s=koneksi.createStatement();
            ResultSet rs=s.executeQuery("SELECT NamaRuangan FROM ruangan ORDER BY NamaRuangan ASC");
            rs.beforeFirst();
            while (rs.next()) {                
                cmbRuangan.addItem(rs.getString("NamaRuangan"));
            }
        } catch (SQLException ex) {
            System.out.println("Ada error "+ex);
        }
        
         cmbNamaBarang.addItem("");
        try {
            Statement s=koneksi.createStatement();
            ResultSet rs=s.executeQuery("SELECT NamaBarang FROM obat ORDER BY NamaBarang ASC");
            rs.beforeFirst();
            while (rs.next()) {                
                cmbNamaBarang.addItem(rs.getString("NamaBarang"));
            }
        } catch (SQLException ex) {
            System.out.println("Ada error "+ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlForm1 = new SIMRS.Apotek.widget.PnlForm();
        label1 = new SIMRS.Apotek.widget.Label();
        label3 = new SIMRS.Apotek.widget.Label();
        txtJumlahStok = new SIMRS.Apotek.widget.TextBox();
        scrollPane1 = new SIMRS.Apotek.widget.ScrollPane();
        tblStokBarang = new SIMRS.Apotek.widget.Table();
        btnSimpan = new SIMRS.Apotek.widget.button2();
        btnUbah = new SIMRS.Apotek.widget.button2();
        btnHapus = new SIMRS.Apotek.widget.button2();
        btnTutup = new SIMRS.Apotek.widget.button2();
        label16 = new SIMRS.Apotek.widget.Label();
        btnCetak = new SIMRS.Apotek.widget.button2();
        cmbRuangan = new javax.swing.JComboBox();
        label2 = new SIMRS.Apotek.widget.Label();
        ruangan = new javax.swing.JTextField();
        kdBarang = new javax.swing.JTextField();
        cmbNamaBarang = new javax.swing.JComboBox();

        setTitle(".: Form Stok Barang");
        setAnimationHide(usu.widget.constan.Animation.HIDE_TO_RIGHT);
        setAnimationShow(usu.widget.constan.Animation.SHOW_FROM_LEFT);
        setResizable(false);
        setUndecorated(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pnlForm1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(57, 190, 53), 3, true));
        pnlForm1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        label1.setText("Nama Barang");
        pnlForm1.add(label1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 120, 110, 20));

        label3.setText("Jumlah Stok");
        pnlForm1.add(label3, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 120, 100, 20));

        txtJumlahStok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtJumlahStokActionPerformed(evt);
            }
        });
        pnlForm1.add(txtJumlahStok, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 140, 100, -1));

        tblStokBarang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblStokBarang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblStokBarangMouseClicked(evt);
            }
        });
        scrollPane1.setViewportView(tblStokBarang);

        pnlForm1.add(scrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, 580, 190));

        btnSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_simpan.png"))); // NOI18N
        btnSimpan.setText("Tambah");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });
        pnlForm1.add(btnSimpan, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 380, -1, -1));

        btnUbah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_edit.png"))); // NOI18N
        btnUbah.setText("Ubah");
        btnUbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUbahActionPerformed(evt);
            }
        });
        pnlForm1.add(btnUbah, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 380, -1, -1));

        btnHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_hapus.png"))); // NOI18N
        btnHapus.setText("Hapus");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });
        pnlForm1.add(btnHapus, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 380, -1, -1));

        btnTutup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_close.png"))); // NOI18N
        btnTutup.setText("Tutup");
        btnTutup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTutupActionPerformed(evt);
            }
        });
        pnlForm1.add(btnTutup, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 380, -1, -1));

        label16.setForeground(new java.awt.Color(255, 255, 255));
        label16.setText("Data Stock Barang");
        label16.setFont(new java.awt.Font("Tahoma", 1, 18));
        pnlForm1.add(label16, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 40, 400, -1));

        btnCetak.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_cetak.png"))); // NOI18N
        btnCetak.setText("Cetak");
        pnlForm1.add(btnCetak, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 380, -1, -1));

        cmbRuangan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbRuanganActionPerformed(evt);
            }
        });
        pnlForm1.add(cmbRuangan, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 140, 130, -1));

        label2.setText("Ruangan");
        pnlForm1.add(label2, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 120, -1, -1));

        ruangan.setForeground(new java.awt.Color(255, 255, 0));
        ruangan.setBorder(null);
        ruangan.setOpaque(false);
        ruangan.setSelectedTextColor(new java.awt.Color(255, 204, 0));
        pnlForm1.add(ruangan, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 140, 60, -1));

        kdBarang.setForeground(new java.awt.Color(255, 255, 51));
        kdBarang.setBorder(null);
        kdBarang.setOpaque(false);
        kdBarang.setSelectedTextColor(new java.awt.Color(255, 255, 51));
        pnlForm1.add(kdBarang, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 110, 90, -1));

        cmbNamaBarang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbNamaBarangActionPerformed(evt);
            }
        });
        pnlForm1.add(cmbNamaBarang, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 140, 170, -1));

        getContentPane().add(pnlForm1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 620, 440));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-621)/2, (screenSize.height-439)/2, 621, 439);
    }// </editor-fold>//GEN-END:initComponents

private void txtJumlahStokActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtJumlahStokActionPerformed
// TODO add your handling code here:
    System.exit(1);
}//GEN-LAST:event_txtJumlahStokActionPerformed

private void btnTutupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTutupActionPerformed
    dispose();
}//GEN-LAST:event_btnTutupActionPerformed

    private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimpanActionPerformed
        // TODO add your handling code here:
        if(btnSimpan.getLabel().equals("Tambah")){
            
            btnSimpan.setLabel("Simpan");
            //buat hidupin
            cmbNamaBarang.setEnabled(true);
            kdBarang.setEnabled(true);
            ruangan.setEnabled(true);
            txtJumlahStok.setEnabled(true);
            cmbRuangan.setEnabled(true);
           
            
            tblStokBarangMouseClicked(null);
        } else if(btnSimpan.getLabel().equals("Simpan")){

    Connection c= KoneksiDatabase.getKoneksi(); //coding simpan
    String sql = "INSERT INTO stok (KdBarang, KdRuangan, JmlStok) VALUES ( ?, ?,?)";
        try {
            PreparedStatement statement= c.prepareStatement(sql);
            statement.setString(1, kdBarang.getText()); //sesuai tipe data di database
            statement.setString(3, txtJumlahStok.getText());
            statement.setString(2, ruangan.getText());
            
            statement.executeUpdate();
            loadDataStokBarang();  //langsung tampil di tabel datanya
            
            JOptionPane.showMessageDialog(this, "Data Berhasil Disimpan");
            btnSimpan.setLabel("Tambah");
            //ini buat enable visible
            cmbNamaBarang.setEnabled(false);
            kdBarang.setText("");
            ruangan.setText("");
            cmbRuangan.setSelectedItem("");
            cmbNamaBarang.setSelectedItem("");
            txtJumlahStok.setEnabled(false);
            cmbRuangan.setEnabled(false);
     
            txtJumlahStok.setText("");
            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data gagal disimpan");
            
        }
        }
    }//GEN-LAST:event_btnSimpanActionPerformed

    private void cmbRuanganActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbRuanganActionPerformed
        // TODO add your handling code here:
        try{
         Statement stm = koneksi.createStatement();
         ResultSet rs = stm.executeQuery("select KdRuangan from ruangan where NamaRuangan= '"+cmbRuangan.getSelectedItem().toString()+"'");
         rs.beforeFirst();
         while(rs.next()){
             ruangan.setText(rs.getString(1).trim());
         }
         rs.close();
         }
         catch(SQLException ex)
         {
             System.out.println("Error : "+ex);
             }
        
    }//GEN-LAST:event_cmbRuanganActionPerformed

    private void tblStokBarangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblStokBarangMouseClicked
        // TODO add your handling code here:
        int i = tblStokBarang.getSelectedRow();
        cmbNamaBarang.setSelectedItem(model.getValueAt(i, 0).toString());
        cmbRuangan.setSelectedItem(model.getValueAt(i, 1).toString());
        txtJumlahStok.setText(model.getValueAt(i, 2).toString());
        
   //     cmbRuangan.getSelectedItem().toString();
    }//GEN-LAST:event_tblStokBarangMouseClicked

private void cmbNamaBarangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbNamaBarangActionPerformed
// TODO add your handling code here:
     try{
         Statement stm = koneksi.createStatement();
         ResultSet rs = stm.executeQuery("select KdBarang from obat where NamaBarang= '"+cmbNamaBarang.getSelectedItem().toString()+"'");
         rs.beforeFirst();
         while(rs.next()){
             kdBarang.setText(rs.getString(1).trim());
         }
         rs.close();
         }
         catch(SQLException ex)
         {
             System.out.println("Error : "+ex);
             }
        
}//GEN-LAST:event_cmbNamaBarangActionPerformed

private void btnUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUbahActionPerformed
// TODO add your handling code here:
    if(btnUbah.getLabel().equals("Ubah")){
            
         cmbNamaBarang.setEnabled(true);
         cmbRuangan.setEnabled(true);
         kdBarang.setEnabled(true);
         ruangan.setEnabled(true);
         txtJumlahStok.setEnabled(true);
         
            btnUbah.setLabel("Simpan");
            tblStokBarangMouseClicked(null);
        } else if(btnUbah.getLabel().equals("Simpan")){
 
    Connection c= KoneksiDatabase.getKoneksi(); //coding ubah
    String sql = "UPDATE stok SET KdRuangan = ? , JmlStok = ? WHERE KdBarang = '"+kdBarang.getText()+"'";
        try {
            PreparedStatement statement= c.prepareStatement(sql);
            
            statement.setString(1, ruangan.getText());
            statement.setString(2, txtJumlahStok.getText());
            statement.executeUpdate();
             //langsung tampil di tabel datanya
            JOptionPane.showMessageDialog(this, "Data Berhasil Diubah");
            btnUbah.setLabel("Ubah");
            //ini buat enable visible
            cmbNamaBarang.setEnabled(false);
            kdBarang.setText("");
            ruangan.setText("");
            cmbRuangan.setSelectedItem("");
            cmbNamaBarang.setSelectedItem("");
            txtJumlahStok.setEnabled(false);
            cmbRuangan.setEnabled(false);
            kdBarang.setEnabled(false);
            ruangan.setEnabled(false);
            txtJumlahStok.setText("");
            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data gagal diubah");
          //  ex.printStackTrace();
        } finally {
                btnUbah.setLabel("Ubah");
                loadDataStokBarang();
                tblStokBarangMouseClicked(null);
            

        }
        }
}//GEN-LAST:event_btnUbahActionPerformed

private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed
// TODO add your handling code here:
     try {
        
            Connection c = KoneksiDatabase.getKoneksi();
            String sql = "DELETE FROM stok WHERE KdBarang = '"+kdBarang.getText()+"' ";
            PreparedStatement p = c.prepareStatement(sql);
            
            p.executeUpdate();
            p.close();
            JOptionPane.showMessageDialog(this, "Data Berhasil di hapus!!!");
            kdBarang.setText("");
            ruangan.setText("");
            txtJumlahStok.setText("");
            cmbRuangan.setSelectedItem("");
            cmbNamaBarang.setSelectedItem("");
            
    } catch (Exception e) {
        System.out.println("Terjadi error pada saat dihapus");
        } finally {
            loadDataStokBarang();
        
    }
}//GEN-LAST:event_btnHapusActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private SIMRS.Apotek.widget.button2 btnCetak;
    private SIMRS.Apotek.widget.button2 btnHapus;
    private SIMRS.Apotek.widget.button2 btnSimpan;
    private SIMRS.Apotek.widget.button2 btnTutup;
    private SIMRS.Apotek.widget.button2 btnUbah;
    private javax.swing.JComboBox cmbNamaBarang;
    private javax.swing.JComboBox cmbRuangan;
    private javax.swing.JTextField kdBarang;
    private SIMRS.Apotek.widget.Label label1;
    private SIMRS.Apotek.widget.Label label16;
    private SIMRS.Apotek.widget.Label label2;
    private SIMRS.Apotek.widget.Label label3;
    private SIMRS.Apotek.widget.PnlForm pnlForm1;
    private javax.swing.JTextField ruangan;
    private SIMRS.Apotek.widget.ScrollPane scrollPane1;
    private SIMRS.Apotek.widget.Table tblStokBarang;
    private SIMRS.Apotek.widget.TextBox txtJumlahStok;
    // End of variables declaration//GEN-END:variables

    private void loadDataStokBarang() {
        model.getDataVector().removeAllElements();
        model.fireTableDataChanged();
        Connection c=KoneksiDatabase.getKoneksi();
        try {
            Statement s= c.createStatement();
            String sql = "SELECT obat.NamaBarang, "
                    + "ruangan.NamaRuangan,"
                    + " stok.JmlStok FROM obat, "
                    + "ruangan, stok "
                    + "WHERE (stok.KdBarang=obat.KdBarang)AND (stok.KdRuangan=ruangan.KdRuangan)";
            ResultSet rs= s.executeQuery(sql);
            
            while (rs.next()){
                Object[] o = new Object[3];
                o[0]=rs.getString("NamaBarang");
                o[1]=rs.getString("NamaRuangan");
                o[2]=rs.getString("JmlStok");
                
                model.addRow(o);
             }
            rs.close();
            s.close();
        } catch (SQLException ex) {
            
        }
    }
    }

